// DlgContextEdit.cpp : implementation file
//

#include "stdafx.h"
#include "mxgtd.h"
#include "DlgContextEdit.h"
#include "afxdialogex.h"
#include "../core/GtdDOMItem.h"
#include "InputBox.h"


// CDlgContextEdit dialog

IMPLEMENT_DYNAMIC(CDlgContextEdit, CDialogEx)

CDlgContextEdit::CDlgContextEdit(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDlgContextEdit::IDD, pParent)
{
	GtdDOM& dom = theApp.getGTDDOM();
	m_ctxlist  =  dom.GetContextList();
	
}

CDlgContextEdit::~CDlgContextEdit()
{
}

void CDlgContextEdit::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_CTXLIST, m_combCtxlist);
}


BEGIN_MESSAGE_MAP(CDlgContextEdit, CDialogEx)
	ON_CBN_SELCHANGE(IDC_COMBO_CTXLIST, &CDlgContextEdit::OnCbnSelchangeComboCtxlist)
	ON_BN_CLICKED(IDOK, &CDlgContextEdit::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_ADD, &CDlgContextEdit::OnBnClickedButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_DEL, &CDlgContextEdit::OnBnClickedButtonDel)
	ON_BN_CLICKED(IDC_BUTTON_ED, &CDlgContextEdit::OnBnClickedButtonEd)
END_MESSAGE_MAP()


// CDlgContextEdit message handlers


void CDlgContextEdit::OnCbnSelchangeComboCtxlist()
{
	// TODO: Add your control notification handler code here

}


BOOL CDlgContextEdit::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  Add extra initialization here
	 LoadComboCtx();
	if(m_combCtxlist.GetCount())
	{
		m_combCtxlist.SetCurSel(0);
	}
	

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgContextEdit::LoadComboCtx()
{
	int nCnt  = 0;
	m_combCtxlist.ResetContent();
	GtdDOM& dom = theApp.getGTDDOM();
	for(GuidList::const_iterator itr = m_ctxlist.begin(); itr!=m_ctxlist.end(); ++itr)
	{
		if( GtdDOMItem* pItem = dom.getItem(*itr) )
		{
			if(GtdContext* pcontext = pItem->toContext() )
			{
				int idx = m_combCtxlist.AddString(pcontext->msName);
						
			}
		}		
	}

	//m_combCtxlist.AddString(_T("New Context...") );
}


void CDlgContextEdit::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	GtdDOM& dom = theApp.getGTDDOM();
	dom.SetContextList(m_ctxlist);
	CDialogEx::OnOK();
}




void CDlgContextEdit::OnBnClickedButtonAdd()
{
	// TODO: Add your control notification handler code here
	GtdDOM& dom = theApp.getGTDDOM();
	CInputBox inputB;
	if(inputB.DoModal()==IDOK)
	{
		GtdContext* pctx = dom.allocateDOMItem<GtdContext>();
		pctx->msName = inputB.m_sInput;
		m_ctxlist.push_back(pctx->m_id);
		LoadComboCtx();
		m_combCtxlist.SetCurSel(m_combCtxlist.GetCount()-1);
	}
	
}


void CDlgContextEdit::OnBnClickedButtonDel()
{
	// TODO: Add your control notification handler code here
	int nSel = m_combCtxlist.GetCurSel();
	if(nSel<m_ctxlist.size())
	{
		m_ctxlist.erase(m_ctxlist.begin()+nSel);
		LoadComboCtx();
		m_combCtxlist.SetCurSel(nSel-1);
	}
	
}


void CDlgContextEdit::OnBnClickedButtonEd()
{
	// TODO: Add your control notification handler code here

	if(GtdContext * pCtx = getCurSel())
	{
		int nSel = m_combCtxlist.GetCurSel();
		CInputBox inputB;
		inputB.m_sInput = pCtx->msName;
		if(inputB.DoModal()==IDOK)
		{
			pCtx->msName=inputB.m_sInput;
		}
		LoadComboCtx();
		m_combCtxlist.SetCurSel(nSel);
	}
	
	
}

GtdContext* CDlgContextEdit::getCurSel() const
{
	GtdDOM& dom = theApp.getGTDDOM();
	int nSel = m_combCtxlist.GetCurSel();
	if(nSel<m_ctxlist.size())
	{
		if( GtdDOMItem * pitem  = dom.getItem(m_ctxlist.at(nSel)) )
		{
			if(GtdContext * pCtx = pitem->toContext())
			{
				return pCtx;
			}
		}
	}
	return NULL;
}
